package com.cyandpos.pad.common.swipemanager;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.changyi.device.swipe.ReadCardInterface;
import com.changyi.device.swipe.ReadCardManager;
import com.changyi.device.swipe.ReadCardResultInterface;
import com.cyandpos.pad.R;
import com.cyandpos.pad.utils.CommonUtils;

/**
 * Created by Administrator on 2017/5/15.
 */

public class SwipeCardCommon implements ISwipeCard {
    private DoAfterSwipeCard receptor;
    private Context mContext;
    private int handId;
    private AlertDialog dialog;
    private TextView textView;
    private Button bt_cancle;
    private ReadCardInterface readCardInterface;
    public SwipeCardCommon(Context context) {
        this.mContext = context;
        RotateAnimation animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f
                , Animation.RELATIVE_TO_SELF, 0.5f);
        // animation.setFillAfter(true);
        animation.setDuration(1000);
        animation.setRepeatMode(Animation.RESTART);
        animation.setRepeatCount(-1);
        View rootView = View.inflate(context, R.layout.dialog_swipecard_paycashcard, null);
        final ImageView imageView = rootView.findViewById(R.id.iv_running);
        textView = rootView.findViewById(R.id.tv_dialogshow_paycashcard);
        bt_cancle=rootView.findViewById(R.id.bt_cancle);
        textView.setText("请刷卡...");
        dialog = new AlertDialog.Builder(mContext).create();
        dialog.setView(rootView);
        dialog.setCancelable(false);
        bt_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fail("取消刷卡");
                colseDriver();
            }
        });
        LinearInterpolator lin = new LinearInterpolator();
        animation.setInterpolator(lin);
        imageView.startAnimation(animation);
    }

    public void setReceptor(DoAfterSwipeCard receptor, int id) {
        this.handId = id;
        this.receptor = receptor;
    }

    public void startSwipe() {
        openDriver();
    }

    @Override
    public void openDriver() {
        dialog.show();
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg_common);
        readCardInterface= ReadCardManager.initReadCard(mContext, CommonUtils.getSystemModel()).init().setReadCardResult(new ReadCardResultInterface() {
            @Override
            public void cardResultSucsses(String cardNumber) {
                Message message = Message.obtain();
                message.what = 0;
                message.obj = cardNumber;
                hander.sendMessage(message);
            }

            @Override
            public void cardResultFail(String msg) {
                Message message = Message.obtain();
                message.what = 1;
                message.obj = msg;
                hander.sendMessage(message);
            }
        });
    }

    public void sumit(String content) {
        if (content != null && !content.equals("")) {
            receptor.doAfterSwipeCard(true, content, handId);
        }
        colseDriver();
    }

    public void fail(String msg) {
        if (msg != null && !msg.equals("")) {
            receptor.doAfterSwipeCard(false, msg, handId);
        }
    }

    @Override
    public void colseDriver() {
        if (dialog != null && dialog.isShowing())
            dialog.dismiss();
           // A8Card.close();
    }

    /**
     * 登录到设备服务
     */
    public void login() {

    }


    public void setDesText(String text) {
        textView.setText(text + "....");
    }

    private Handler hander = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    sumit(msg.obj+"");
                    break;
                case 1:
                    fail(msg.obj+"");
                    break;
            }
            return true;
        }
    });

    public interface DoAfterSwipeCard {
        void doAfterSwipeCard(boolean isSuccess, String result, int handId);
    }
}
